<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!showAddress" class="none">
        您需要先添加收货地址才可提交订单。
      </div>
      <ul v-if="showAddress">
        <li>
          <span>收<i />货<i />人：</span>{{ showAddress.receiver }}
        </li>
        <li><span>联系方式：</span>{{ showAddress.contact }}</li>
        <li>
          <span>收货地址：</span
          >{{
            showAddress.fullLocation.replace(/ /g, '') + showAddress.address
          }}
        </li>
      </ul>
      <a
        v-if="showAddress"
        href="javascript:;"
        @click="openAddAddressFn(showAddress)"
        >修改地址</a
      >
    </div>
    <div class="action">
      <XtxButton class="btn" @click="openDialog">切换地址</XtxButton>
      <XtxButton class="btn" @click="openAddAddressFn({})">添加地址</XtxButton>
    </div>
  </div>
  <!-- 切换地址对话框 -->
  <XtxDialog
    title="切换收货地址"
    v-model:visible="visibleDialog"
    @update:visible="changeVisvle"
  >
    <div
      @click="selectedAddressFn(item)"
      :class="{ active: selectedAddress && selectedAddress.id === item.id }"
      class="text item"
      v-for="item in address"
      :key="item.id"
    >
      <ul>
        <li>
          <span>收<i />货<i />人：</span>{{ item.receiver }}
        </li>
        <li><span>联系方式：</span>{{ item.contact }}</li>
        <li>
          <span>收货地址：</span
          >{{ item.fullLocation.replace(/ /g, '') + item.address }}
        </li>
      </ul>
      <XtxButton type="primary" size="mini" @click="deletteAdderss(item.id)"
        >删除</XtxButton
      >
    </div>

    <template #footer>
      <XtxButton
        type="gray"
        style="margin-right: 20px"
        @click="visibleDialog = false"
        >取消</XtxButton
      >
      <XtxButton type="primary" @click="confirmAddress">确认</XtxButton>
    </template>
  </XtxDialog>
  <!-- 添加地址对话框 -->
  <AddressEdit @on-success="successFn" ref="addressEditCom"> </AddressEdit>
</template>
<script>
import { ref } from 'vue'
import { deleteAddress } from '@/api/order.js'
import Message from '@/components/library/Message'
import AddressEdit from '@/views/member/pay/components/address-edit.vue'
export default {
  name: 'CheckoutAddress',
  props: {
    address: {
      type: Array,
      default: () => []
    }
  },
  components: { AddressEdit },
  emits: ['change'],
  setup(props, { emit }) {
    // 显示地址\
    const showAddress = ref(null)
    setTimeout(() => {
      if (props.address.length > 0) {
        const defaultAddress = props.address.find(
          (item) => item.isDefault === 1
        )
        if (defaultAddress) {
          showAddress.value = defaultAddress
        } else {
          // eslint-disable-next-line vue/no-setup-props-destructure
          showAddress.value = props.address[0]
          // 默认通知父组件一个收货地址id
        }
      }
      emit('change', showAddress.value && showAddress.value.id)
    }, 500)

    // 显示隐藏
    const visibleDialog = ref(false)
    const changeVisvle = (newVal) => {
      visibleDialog.value = false
    }
    // 记录当前你选中的地址的信息
    const selectedAddress = ref(null)
    const selectedAddressFn = (item) => {
      selectedAddress.value = item
    }
    const confirmAddress = () => {
      // 把选择的地址id通知结算组件
      if (selectedAddress.value) {
        // 显示的地址换成选中的地址
        showAddress.value = selectedAddress.value
        emit('change', selectedAddress.value.id)
        visibleDialog.value = false
      } else {
        Message('warn', '您没有选中地址')
      }
    }
    // 删除地址
    const deletteAdderss = (id) => {
      deleteAddress(id).then(({ result }) => {
        const id = props.address.findIndex((item) => item.id === result.id)
        // eslint-disable-next-line vue/no-mutating-props
        props.address.splice(id, 1)
      })
    }
    // 打开对话框
    const openDialog = () => {
      // 打开对话框把选中的数据变为空,
      selectedAddress.value = null
      visibleDialog.value = true
    }
    // 打开添加编辑地址收获组件
    const addressEditCom = ref(null)
    const openAddAddressFn = (address) => {
      // 修改添加在一个组件(address-edit)进行，通过有无对象判断
      addressEditCom.value.open(address)
    }
    // 修改或添加成功给props.list添加数据
    const successFn = (form) => {
      const id = props.address.findIndex((item) => item.id === form.id)
      if (id > -1) {
        console.log(2)
        // eslint-disable-next-line vue/no-mutating-props
        props.address.splice(id, 1, form)
        showAddress.value = props.address[id]
      } else {
        console.log(1)
        // 如果是添加，就给list添加一天
        const jsonStr = JSON.stringify(form)
        // eslint-disable-next-line vue/no-mutating-props
        props.address.unshift(JSON.parse(jsonStr))
      }
    }
    return {
      visibleDialog,
      changeVisvle,
      showAddress,
      selectedAddress,
      confirmAddress,
      deletteAdderss,
      selectedAddressFn,
      openDialog,
      openAddAddressFn,
      addressEditCom,
      successFn
    }
  }
}
</script>
<style scoped lang="less">
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    display: flex;
    justify-content: space-between;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
</style>
